#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/8/4 22:55
# @USER    : Shengji He
# @File    : ImplementTrie.py
# @Software: PyCharm
# @Version  : Python-
# @TASK:


class Trie:
    """
    Implement a trie with insert, search, and startsWith methods.

    Example:
        Trie trie = new Trie();

        trie.insert("apple");
        trie.search("apple");   // returns true
        trie.search("app");     // returns false
        trie.startsWith("app"); // returns true
        trie.insert("app");
        trie.search("app");     // returns true
    Note:
        - You may assume that all inputs are consist of lowercase letters a-z.
        - All inputs are guaranteed to be non-empty strings.
    """
    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.lookup = {}

    def insert(self, word: str) -> None:
        """
        Inserts a word into the trie.
        """
        tree = self.lookup
        for a in word:
            if a not in tree:
                tree[a] = {}
            tree = tree[a]
        tree['#'] = '#'

    def search(self, word: str) -> bool:
        """
        Returns if the word is in the trie.
        """
        tree = self.lookup
        for a in word:
            if a not in tree:
                return False
            tree = tree[a]
        if '#' in tree:
            return True
        else:
            return False

    def startsWith(self, prefix: str) -> bool:
        """
        Returns if there is any word in the trie that starts with the given prefix.
        """
        tree = self.lookup
        for a in prefix:
            if a not in tree:
                return False
            tree = tree[a]

        return True


if __name__ == '__main__':

    # Your Trie object will be instantiated and called as such:
    # obj = Trie()
    # obj.insert(word)
    # param_2 = obj.search(word)
    # param_3 = obj.startsWith(prefix)
    obj = Trie()
    obj.insert('apple')
    obj.search('apple')
    obj.search('app')
    obj.startsWith('app')
    obj.insert('app')
    obj.search('app')
    print('done')
